# cmake最低版本需求，不加入此行会受到警告信息
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)

# 项目名称，要生成的可执行文件名
PROJECT(iec101s) 

# 设置可执行文件的输出目录， PROJECT_SOURCE_DIR 一般为 CMakeLists.txt 所在目录
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})

# 告知当前使用的是交叉编译方式
SET(CMAKE_SYSTEM_NAME Linux)

if(${ARM})
	# 如果使能了 ARM 变量，设置交叉编译器
	SET(CMAKE_C_COMPILER "/opt/poky/1.7/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc")
	
	# 设置交叉编译链库文件根路径 CMAKE_FIND_ROOT_PATH
	SET(ROOT_PATH    "/opt/poky/1.7/sysroots/cortexa9hf-vfp-neon-poky-linux-gnueabi/")

	# 交叉编译时的编译选项 
	SET(ARM_CFLAG "-march=armv7-a -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a9 --sysroot=${ROOT_PATH} -pipe -feliminate-unused-debug-types")

	SET(PROG_CFLAG "ARM")
else()
	# 设置 x86 平台的编译器
	SET(CMAKE_C_COMPILER "gcc")
	SET(ARM_CFLAG "")
	SET(PROG_CFLAG "x86_64")
endif()


# 设置 gcc 配置  -g
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ARM_CFLAG} -lmysqlclient -lpthread -std=gnu99 -Wall -O2")

# 打印一下变量
message("\t${PROG_CFLAG} flags:\r\n\t${CMAKE_C_FLAGS}")


# 将目录中的源文件名称赋值给变量
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/project             MAIN_SRCS) 
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/../../60870-5       LIB_IEC_SRCS) 
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/../../60870-5/10x   LIB_10x_SRCS) 
AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/../../60870-5/iec   LIB_BASE_SRCS) 

#生成链接库, add_library 命令将目录中的源文件编译为静态链接库。
add_library(iec10x   ${LIB_IEC_SRCS} ${LIB_10x_SRCS} ${LIB_BASE_SRCS})


# 将源文件编译成可执行文件。
ADD_EXECUTABLE(iec101s  ${MAIN_SRCS})

# 头文件包含路径
include_directories("${PROJECT_SOURCE_DIR}/project")
include_directories("${PROJECT_SOURCE_DIR}/../../60870-5")
include_directories("${PROJECT_SOURCE_DIR}/../../60870-5/10x")
include_directories("${PROJECT_SOURCE_DIR}/../../60870-5/type")

# 添加链接库
target_link_libraries(iec101s     pthread iec10x) 

